package com.lee.dutylower.db.greenEntity;

import android.support.annotation.NonNull;
import android.util.Log;


import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinEntity;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.ToOne;

import java.util.List;
import com.lee.dutylower.db.BaseDB;
import com.lee.dutylower.db.greendao.DaoSession;
import com.lee.dutylower.db.greendao.SentryDao;
import com.lee.dutylower.db.greendao.SquadDao;
import com.lee.dutylower.db.greendao.SoldierDao;
import com.lee.dutylower.domain.SoldierInUseEnum;

/**
 * Created by lijie on 2017/4/22.
 */

@Entity
public class Soldier extends BaseDB implements Comparable<Soldier>{
    @Id(autoincrement = true)
    private Long sID;

    @Property
    private Long squadID;

    @ToOne(joinProperty = "squadID")
    private Squad squad;

    @Property
    private String sName;

    @ToMany
    @JoinEntity(entity = SoldierSentry.class,sourceProperty = "soldierId",targetProperty = "sentryId")
    private List<Sentry> sentryGroup;

    @Property
    private Integer grade;

    @Property
    private Integer inUse;

    @Property
    private Integer guardTimes;

    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated(hash = 1475791829)
    private transient SoldierDao myDao;

    @Generated(hash = 1222221301)
    public Soldier(Long sID, Long squadID, String sName, Integer grade, Integer inUse,
            Integer guardTimes) {
        this.sID = sID;
        this.squadID = squadID;
        this.sName = sName;
        this.grade = grade;
        this.inUse = inUse;
        this.guardTimes = guardTimes;
    }

    @Generated(hash = 1815905691)
    public Soldier() {
    }

    public Long getSID() {
        return this.sID;
    }

    public void setSID(Long sID) {
        this.sID = sID;
    }

    public Long getSquadID() {
        return this.squadID;
    }

    public void setSquadID(Long squadID) {
        this.squadID = squadID;
    }

    public String getSName() {
        return this.sName;
    }

    public void setSName(String sName) {
        this.sName = sName;
    }

    public Integer getGrade() {
        return this.grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    public Integer getGuardTimes() {
        if (guardTimes == null){
            guardTimes = 0;
        }
        return this.guardTimes;
    }

    public void setGuardTimes(Integer guardTimes) {
        this.guardTimes = guardTimes;
    }

    @Generated(hash = 2016322389)
    private transient Long squad__resolvedKey;

    /** To-one relationship, resolved on first access. */
    @Generated(hash = 1519719723)
    public Squad getSquad() {
        Long __key = this.squadID;
        if (squad__resolvedKey == null || !squad__resolvedKey.equals(__key)) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            SquadDao targetDao = daoSession.getSquadDao();
            Squad squadNew = targetDao.load(__key);
            synchronized (this) {
                squad = squadNew;
                squad__resolvedKey = __key;
            }
        }
        return squad;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 246468666)
    public void setSquad(Squad squad) {
        synchronized (this) {
            this.squad = squad;
            squadID = squad == null ? null : squad.getSuqadId();
            squad__resolvedKey = squadID;
        }
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 1750306969)
    public List<Sentry> getSentryGroup() {
        if (sentryGroup == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            SentryDao targetDao = daoSession.getSentryDao();
            List<Sentry> sentryGroupNew = targetDao._querySoldier_SentryGroup(sID);
            synchronized (this) {
                if (sentryGroup == null) {
                    sentryGroup = sentryGroupNew;
                }
            }
        }
        return sentryGroup;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 1826581509)
    public synchronized void resetSentryGroup() {
        sentryGroup = null;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 1975836505)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getSoldierDao() : null;
    }



    @Override
    public int compareTo(@NonNull Soldier o) {
        if (this.guardTimes!=0 || o.getGuardTimes()!=0){
            if ((this.guardTimes - o.getGuardTimes()) == 0){
                if ((this.getSentryGroup().size() - o.getSentryGroup().size()) != 0){
                    return this.getSentryGroup().size() - o.getSentryGroup().size();
                }
                return (int) (this.sID - o.getSID());
            }
            return this.guardTimes - o.getGuardTimes();
        }
        if ((this.getSentryGroup().size() - o.getSentryGroup().size()) != 0){
            return this.getSentryGroup().size() - o.getSentryGroup().size();
        }
        return (int) (this.sID - o.getSID());
    }

    @Override
    public boolean equals(Object obj) {
        boolean result = false;
        if (obj instanceof Soldier){
            Soldier out = (Soldier) obj;
            Log.e("SQLTEST",sName+"");
            if (this.getSID().longValue() == out.getSID().longValue()){
                result = true;
            }

        }
        return result;
    }

    @Override
    public int hashCode() {
        return (int) (this.getSID()+0);
    }

    public boolean canGuard(Sentry sentry){
        boolean result = false;
        if (SoldierInUseEnum.InUse.getCode() == inUse){
            return false;
        }
        List<Sentry> sentrs = this.getSentryGroup();
        for (Sentry sentr : sentrs) {
            if (sentr.getSentry_id().longValue() == sentry.getSentry_id().longValue())
                result = true;
        }
        this.resetSentryGroup();
        return result;
    }
    public boolean canGuard(Sentry sentry,boolean ignoreInUse){
        boolean result = false;
        if (ignoreInUse){
            List<Sentry> sentrs = this.getSentryGroup();
            for (Sentry sentr : sentrs) {
                if (sentr.getSentry_id().longValue() == sentry.getSentry_id().longValue())
                    result = true;
            }
        }
        this.resetSentryGroup();
        return result;
    }

    public Integer getInUse() {
        return this.inUse;
    }

    public void setInUse(Integer inUse) {
        this.inUse = inUse;
    }

    @Override
    public String toString() {
        return "Soldier{" +
                "sID=" + sID +
                ", sName='" + sName + '\'' +
                ", sentryGroup=" + getSentryGroup().size() +
                ",guard times="+guardTimes+
                '}';
    }
}
